/**************************************************************************(C)*
 *
 *  This file is part of the GreenHills project
 *  Copyright (C) 2009  Tieto Corporation
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  Not for commmercial use. Only with Tieto permission.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 *************************************************************************(C)*/
#include <QtGui/QApplication>
#include <QStringList>
#include <QTimer>
#include <iostream>

#include "mainwindow.h"

#include "util/serialport.h"
#include "util/lineterminal.h"
#include "gps/nmeaparser.h"
#include "client/core/client.h"

using namespace greenhills::client;
/*
//== Tests =====================================================================

#include "../../util/serialport.h"
#include "../../util/lineterminal.h"
#include "../../gps/nmeaparser.h"
#include "../../gps/gps.h"
#include "gpstestwidget.h"
void testGps()
{
    using namespace util;
    using namespace gps;

    CSerialPort *serialPort = CSerialPort::instance();
    CLineTerminal *lineTerminal = new CLineTerminal(QApplication::instance());
    CNMEAParser *nmeaParser = new CNMEAParser(QApplication::instance());
    QObject::connect(serialPort, SIGNAL(dataReceived(QByteArray)),
                     lineTerminal, SLOT(onDataReceived(QByteArray)));
    QObject::connect(lineTerminal, SIGNAL(lineReceived(QString)),
                     nmeaParser, SLOT(onLineReceived(QString)));

    CGPS *gps = new CGPS(nmeaParser);
    QObject::connect(
        nmeaParser, SIGNAL(GPGGAreceived(QTime,CGPSPosition,CGPSFix,
            int,float,float,float,float,int)),
        gps, SLOT(onGPGGAreceived(QTime,CGPSPosition,CGPSFix,
            int,float,float,float,float,int)));
    QObject::connect(
        nmeaParser, SIGNAL(GPVTGreceived(float,float,float,float)),
        gps, SLOT(onGPVTGreceived(float,float,float,float)));

    CGPSTestWidget *gpsTestWidget =
            new CGPSTestWidget(serialPort, lineTerminal, nmeaParser);

    gpsTestWidget->show();
}


#include <ostream>
#include <istream>
#include "../../util/binstream.h"
#include "../../util/memstreambuf.h"
#include "../../event/loginevent.h"
void testEvents()
{
    using namespace greenhills::event;
    using namespace util;

    memstreambuf buffer;//((unsigned int)0,10);
    {
        obinstream bos(&buffer);
        CLoginRequestEvent event("Username", "Password");
        CEvent::serialize(&event, bos);
    }
    {
        memstreambuf buffer2(buffer.data(), buffer.length());
        ibinstream bos(&buffer2);
        CEvent *event = CEvent::deserialize(bos);
        Q_ASSERT(event != 0);
    }
}

#include "../../net/msgclientsocketconnection.h"
#include "../../io/ioexception.h"
#include "testdatalistener.h"
#include "connectionprocessor.h"
void testNet()
{
    using namespace net;

    CMsgClientSocketConnection *connection= new CMsgClientSocketConnection();

    CTestDataListener *listener = new CTestDataListener();
    connection->addDataListener(listener);

    cout << "Connecting" << endl;

    try {
        connection->open("localhost", 5000);
    } catch (io::CIOException ioe) {
        cout << ioe.toString() << endl;
        exit(1);
    }

    cout << "Connected." << endl;

    QTimer *processTimer = new QTimer();
    CConnectionProcessor *connectionProcessor
            = new CConnectionProcessor(connection);
    processTimer->setSingleShot(false);
    processTimer->setInterval(10);
    processTimer->start();
    QObject::connect(processTimer, SIGNAL(timeout(void)),
            connectionProcessor, SLOT(process(void)));

    char data[10000];
    for (int i = 0; i < 10000; i++) {
        data[i] = i % 125;
    }

    cout << "Sending message." << endl;
    connection->sendMessage(data, sizeof(data));
    cout << "Message sent." << endl;
}

#include "compas/compas.h"
void testCompas()
{
    CCompas *compas = new CCompas();
    compas->show();
}
*/
//== End of Tests ==============================================================

int main(int argc, char *argv[])
{
    Q_UNUSED(argc);
    Q_UNUSED(argv);

    QApplication a(argc, argv);

//    testGps();
//    testEvents();
//    testNet();
//    testCompas();

    using namespace greenhills::client;
    using namespace greenhills::util;
    using namespace greenhills::gps;

    CSerialPort *serialPort = CSerialPort::instance();
    CLineTerminal *lineTerminal = new CLineTerminal(QApplication::instance());
    CNMEAParser *nmeaParser = new CNMEAParser(QApplication::instance());

    QObject::connect(serialPort, SIGNAL(dataReceived(QByteArray)),
        lineTerminal, SLOT(onDataReceived(QByteArray)));
    QObject::connect(lineTerminal, SIGNAL(lineReceived(QString)),
        nmeaParser, SLOT(onLineReceived(QString)));


    greenhills::client::core::CClient client;
    QObject::connect( nmeaParser, SIGNAL(GPGGAreceived(
        QTime,greenhills::gps::CGPSPosition,greenhills::gps::CGPSFix,
        int,float,float,float,float,int)),
        &client, SLOT(onGPGGAreceived(
        QTime,greenhills::gps::CGPSPosition,greenhills::gps::CGPSFix,
        int,float,float,float,float,int)));

    QObject::connect(nmeaParser,SIGNAL(GPVTGreceived(float,float,float,float)),
        &client,SLOT(onGPVTGreceived(float,float,float,float)));

    CMainWindow mainWindow(serialPort, lineTerminal, nmeaParser, client);
    mainWindow.show();

    return a.exec();
}
